﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Text.RegularExpressions;

using DevExpress.Utils;
using DbHelp;
namespace IotherRis
{
    public partial class Fr_BaseinfoDetail : DevExpress.XtraEditors.XtraForm
    {
        public Fr_BaseinfoDetail()
        {
            InitializeComponent();
        }
        string mIid;
        uDataSetC mUds_load;
       // Fr_BaseInfo mDj;

        public Fr_BaseinfoDetail(object pSend, object pParam)
        {
            InitializeComponent();
            mIid = (string)pParam;
            //mDj = (Fr_BaseInfo)pSend;
        }

        private void dengji_sz_detail_Load(object sender, EventArgs e)
        {

            txt_type.Visible = false;

            uDataSetC mUds_lue_type = new uDataSetC("select * from PI_BASEINFO where ipid=0", AccessHelper.mReportDb);
            lue_type.Properties.DataSource = mUds_lue_type.Tables[0];
            lue_type.Properties.ValueMember = "IID";
            lue_type.Properties.DisplayMember = "CPARAMTYPE";

            string mSql_load =  @"select * from PI_BASEINFO where iid=" + mIid + "" ;
            mUds_load = new uDataSetC(mSql_load,AccessHelper.mReportDb);
            if (mUds_load.RowCount() > 0)
            {
                lue_type.Text = mUds_load.GetItemString(1, "CPARAMTYPE");
                txt_code.Text = mUds_load.GetItemString(1, "CPARAMCODE");
                txt_desc.Text = mUds_load.GetItemString(1, "CPARAMDESC");
                txt_cother.Text = mUds_load.GetItemString(1, "COTHER");
                txt_sort.Text = mUds_load.GetItemString(1, "IORDERNUM");
                txt_classname.Text = mUds_load.GetItemString(1, "CCLASSNAME");
                txt_classdata.Text = mUds_load.GetItemString(1, "CCLASSDATA");
            }

        }

        private void ckb_type_CheckedChanged(object sender, EventArgs e)
        {
            if (ckb_type.Checked)
            {
                txt_type.Visible = true;
                lue_type.Visible = false;
                lue_type.Enabled = false;
                txt_code.Text = "--";
                txt_code.Enabled = false;
            }
            else
            {
                txt_type.Visible = false;
                lue_type.Visible = true; ;
                lue_type.Enabled = true;
                txt_code.Enabled = true;
            }
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            //mDj.MIsTrue = false;
            this.Close();
        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
            string mStrErr = "";
            if (lue_type.Text == lue_type.Properties.NullText & ckb_type.Checked == false)
            {
                mStrErr = mStrErr + "\r\n类别不能为空";
            }
            if (string.IsNullOrEmpty(txt_code.Text))
            {
                mStrErr = mStrErr + "\r\n属性编码不能为空";
            }
            if (string.IsNullOrEmpty(txt_desc.Text))
            {
                mStrErr = mStrErr + "\r\n属性描述不能为空";
            }
            int mResult;
            if (!string.IsNullOrEmpty(txt_sort.Text))
            {
                if (!int.TryParse(txt_sort.Text, out mResult))
                {
                    XtraMessageBox.Show("必须为数字");
                    return;
                }
            }

            //验证type下code的唯一性
            uDataSetC mUds_yz;
            if (ckb_type.Checked)
            {
                string mSql_yz_type = @"select * from PI_BASEINFO where cparamtype=" + txt_type.Text + "" ;
                mUds_yz = new uDataSetC(mSql_yz_type, AccessHelper.mReportDb);

                if (mUds_yz.RowCount() > 0)
                {
                   XtraMessageBox.Show("已存在该类型,不能重复添加");
                    return;
                }
            }
            if (mUds_load.RowCount() < 1)
            {
                string mSql_yz_code =  @"select * from PI_BASEINFO where cparamtype='" + lue_type.Text + "' and cparamcode ='" + txt_code.Text.Trim() + "'" ;
                mUds_yz = new uDataSetC(mSql_yz_code, AccessHelper.mReportDb);
                if (mUds_yz.RowCount() > 0)
                {
                    XtraMessageBox.Show("在该类型下已存在类别编码为 " + txt_code.Text + " 的项不能重复添加");
                    return;
                }
            }
            

            if (!string.IsNullOrEmpty(mStrErr)) {
                XtraMessageBox.Show(mStrErr);
                return;
            }

            if (mUds_load.RowCount() < 1)
            {
                mUds_load.InsertRow();
            }
            if (ckb_type.Checked)
            {
                mUds_load.SetItem(1, "CPARAMTYPE", txt_type.Text);
                mUds_load.SetItem(1, "IPID", 0);
            }
            else
            {
                mUds_load.SetItem(1, "CPARAMTYPE", lue_type.Text);
                mUds_load.SetItem(1, "IPID", lue_type.EditValue.ToString());
            }

            mUds_load.SetItem(1, "CPARAMCODE", txt_code.Text);
            mUds_load.SetItem(1, "CPARAMDESC", txt_desc.Text);
            mUds_load.SetItem(1, "COTHER", txt_cother.Text);

            mUds_load.SetItem(1, "CCLASSNAME", txt_classname.Text);
            mUds_load.SetItem(1, "CCLASSDATA", txt_classdata.Text);
            if (!string.IsNullOrEmpty(txt_sort.Text))
            {
                mUds_load.SetItem(1, "IORDERNUM", txt_sort.Text);
            }


            if (mUds_load.Update(AccessHelper.mReportDb) == 0)
            {

                if (!ckb_close.Checked)
                {
                   // mDj.MIsTrue = true;
                    this.Close();
                }
                else
                {
                    mUds_load.Tables[0].Rows.RemoveAt(0);
                    mUds_load.InsertRow();
                    txt_code.Text = "";
                    txt_cother.Text = "";
                    txt_desc.Text = "";
                    txt_sort.Text = "";
                    txt_classdata.Text = "";
                    txt_classname.Text = "";
                }
                MessageBox.Show("保存成功");
            }
            else
            {
                XtraMessageBox.Show("保存失败");
                return;
            }
        }


    }
}